package cn.dankal.lequ.ui.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dankal.lequ.R;
import cn.dankal.lequ.commom.adapter.BaseAdapter.ViewHolder;
import cn.dankal.lequ.commom.adapter.BaseAdapter.recyclerview.CommonAdapter;
import cn.dankal.lequ.commom.adapter.RecyclerViewAdapter;
import cn.dankal.lequ.commom.util.LogUtil;
import cn.dankal.lequ.commom.util.MyList;

public class HistoryActivity extends AppCompatActivity {
    @Bind(R.id.tab_layout)
    TabLayout tab_layout;
    @Bind(R.id.view_pager)
    ViewPager view_pager;
    LinearLayoutManager linearLayoutManager;
    @Bind(R.id.et_search)
    EditText et_search;
    RecyclerView recyclerview, recyclerview1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        ButterKnife.bind(this);
        initTabLayout();
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }

    @OnClick({R.id.bt_back, R.id.et_search, R.id.iv_search})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                this.finish();
                break;
            case R.id.et_search:
                et_search.setCursorVisible(true);
                break;
            case R.id.iv_search:
                LogUtil.showToast(et_search.getText().toString());
                et_search.setCursorVisible(false);
                break;
            default:
                break;
        }

    }

    private void initTabLayout() {
        MyList<RecyclerView> recyclerviews = new MyList<>();
        MyList<String> tablayout_titles = new MyList<>();
        tablayout_titles.add("推荐标签", "搜索历史");
        initRecyclerView();
        initRecyclerView1();
        recyclerviews.add(recyclerview, recyclerview1);
        for (int i = 0; i < tablayout_titles.size(); i++) {
            tab_layout.addTab(tab_layout.newTab().setText((String) tablayout_titles.get(i)));
        }
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(recyclerviews, tablayout_titles);
        view_pager.setAdapter(adapter);
        tab_layout.setupWithViewPager(view_pager);
        tab_layout.setTabsFromPagerAdapter(adapter);
    }

    private void initRecyclerView() {
        recyclerview = new RecyclerView(this);
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);
        recyclerview.setHasFixedSize(true);

        List<Integer> mDatas = new ArrayList<>();
        mDatas.add(1);
        mDatas.add(2);
        mDatas.add(3);
        mDatas.add(4);
        mDatas.add(5);
        mDatas.add(6);
        mDatas.add(7);
        mDatas.add(8);
        recyclerview.setAdapter(new CommonAdapter<Integer>(this, R.layout.flowtag, mDatas) {
            @Override
            public void convert(ViewHolder holder, Integer i) {
            }
        });
    }

    private void initRecyclerView1() {
        recyclerview1 = new RecyclerView(this);
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerview1.setLayoutManager(linearLayoutManager);
        recyclerview1.setHasFixedSize(true);

        List<Integer> mDatas = new ArrayList<>();
        mDatas.add(1);
        mDatas.add(2);
        mDatas.add(3);
        mDatas.add(4);
        mDatas.add(5);
        mDatas.add(6);
        mDatas.add(7);
        mDatas.add(8);
        recyclerview1.setAdapter(new CommonAdapter<Integer>(this, R.layout.item_history, mDatas) {
            @Override
            public void convert(ViewHolder holder, final Integer i) {
                holder.setOnClickListener(R.id.rl_history, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        LogUtil.showToast(String.valueOf(i));
                    }
                });
            }
        });
    }
}
